package com.dotstone.chipism.activity;

import com.dotstone.chipism.R;
import com.dotstone.chipism.view.slidingfragmenu.BarUtils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class RemoteCWLightActivity extends BaseActivity {
	private LinearLayout mBackL;
	private TextView mStatusBar;
	private Button mPowerB;
	private TextView mTitleTv;
	private boolean power = false;
	private Button mMoreB;
	private Dialog dialog;
	private Button sceneB1;
	private Button sceneB2;
	private Button sceneB3;
	private Button sceneB4;
	private Button sceneB5;
	private Button sceneB6;
	private RelativeLayout mF;
	private RelativeLayout mF1;
	private RelativeLayout mS;
	private RelativeLayout mS1;
	private int o_x = 332;
	private int o_y = 333;
	private float current_degree;
	private int tempX1;
	private int tempY1;
	private Button mJump;
	private long lastTime;
	private long currentTime;
	private long lastTime1;
	private long currentTime1;
	private int level;
	private int pixel;
	private Drawable d;
	private Bitmap bm;
	private int r;
	private int g;
	private int b;
	private int inner;

	@Override
	public void widgetClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_layout:
			finish();
			break;
		case R.id.power_b:
			if (power) { // 当前打开 发送关指令
				mPowerB.setBackgroundResource(R.drawable.kaiguan22x);
			} else { // 当前关闭 发送开的指令
				mPowerB.setBackgroundResource(R.drawable.kaiguan12x);
			}
			power = !power;
			break;
		case R.id.more_btn:
			// 显示选择弹出框
			dialog = new Dialog(RemoteCWLightActivity.this,
					R.style.MyDialogStyle);
			dialog.setContentView(R.layout.dialog_light_scene1);
			Window dialogWindow = dialog.getWindow();
			WindowManager.LayoutParams lp = dialogWindow.getAttributes();
			dialogWindow.setGravity(Gravity.CENTER);
			lp.width = LayoutParams.FILL_PARENT;
			dialogWindow.setAttributes(lp);
			sceneB1 = (Button) dialog.findViewById(R.id.scene_btn1);
			sceneB2 = (Button) dialog.findViewById(R.id.scene_btn2);
			sceneB3 = (Button) dialog.findViewById(R.id.scene_btn3);
			sceneB4 = (Button) dialog.findViewById(R.id.scene_btn4);
			sceneB5 = (Button) dialog.findViewById(R.id.scene_btn5);
			sceneB6 = (Button) dialog.findViewById(R.id.scene_btn6);
			sceneB1.setOnClickListener(this);
			sceneB2.setOnClickListener(this);
			sceneB3.setOnClickListener(this);
			sceneB4.setOnClickListener(this);
			sceneB5.setOnClickListener(this);
			sceneB6.setOnClickListener(this);
			dialog.show();
			break;
		case R.id.scene_btn1:
			Log.i("wmy", "scene_btn1");
			break;
		case R.id.scene_btn2:
			Log.i("wmy", "scene_btn2");
			break;
		case R.id.scene_btn3:
			Log.i("wmy", "scene_btn3");
			break;
		case R.id.scene_btn4:
			Log.i("wmy", "scene_btn4");
			break;
		case R.id.scene_btn5:
			Log.i("wmy", "scene_btn5");
			break;
		case R.id.scene_btn6:
			Log.i("wmy", "scene_btn6");
			break;

		default:
			break;
		}
	}

	@Override
	public void initParms(Bundle parms) {
		// TODO Auto-generated method stub

	}

	@Override
	public View bindView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int bindLayout() {
		// TODO Auto-generated method stub
		return R.layout.activity_cw_light;
	}

	@Override
	public void initView(View view) {
		// TODO Auto-generated method stub
		mBackL = $(R.id.back_layout);
		mStatusBar = $(R.id.tv_statusBar_mian);
		mTitleTv = $(R.id.title_tv);
		mTitleTv.setText("色温灯");
		mPowerB = $(R.id.power_b);
		mMoreB = $(R.id.more_btn);
		mF = (RelativeLayout) findViewById(R.id.rl_father);
		mF1 = (RelativeLayout) findViewById(R.id.rl_father1);
		mS = (RelativeLayout) findViewById(R.id.rl_mezi);
		mS1 = (RelativeLayout) findViewById(R.id.rl_mezi1);
		lastTime = System.currentTimeMillis();
		currentTime = System.currentTimeMillis();
		lastTime1 = System.currentTimeMillis();
		currentTime1 = System.currentTimeMillis();
		d = mF.getBackground();
		bm = drawableToBitmap(d);
		boolVersion();
		mBackL.setOnClickListener(this);
		mPowerB.setOnClickListener(this);
		mMoreB.setOnClickListener(this);
		mF.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent e) {
				// TODO Auto-generated method stub
				int action = e.getAction();
				switch (action) {
				case MotionEvent.ACTION_DOWN:
					Log.i("wmy", "点击" + e.getX() + " " + e.getY());
					if (adjustArea(o_x, o_y, e.getX(), e.getY())) {
						current_degree = detaDegree(o_x, o_y, e.getX(),
								e.getY());
						// 根据象限求坐标
						currentTime = System.currentTimeMillis();
						getButtonXY(current_degree);
						mS.setX(tempX1 - 12);
						mS.setY(tempY1 - 12);
						mS.postInvalidate();
						getColor();
					} else {
						return false;
					}
					break;
				case MotionEvent.ACTION_MOVE:
					if (adjustArea(o_x, o_y, e.getX(), e.getY())) {
						current_degree = detaDegree(o_x, o_y, e.getX(),
								e.getY());
						// 根据象限求坐标
						currentTime = System.currentTimeMillis();
						if (currentTime - lastTime > 150) {
							lastTime = currentTime;
							getColor();
						}
						getButtonXY(current_degree);
						mS.setX(tempX1 - 12);
						mS.setY(tempY1 - 12);
						mS.postInvalidate();
					} else {
						return false;
					}
					break;
				case MotionEvent.ACTION_UP:
					if (adjustArea(o_x, o_y, e.getX(), e.getY())) {
						Log.i("wmy", "上提" + e.getX() + " " + e.getY());
						current_degree = detaDegree(o_x, o_y, e.getX(),
								e.getY());
						// 根据象限求坐标
						getButtonXY(current_degree);
						mS.setX(tempX1 - 12);
						mS.setY(tempY1 - 12);
						mS.postInvalidate();
						getColor();
					} else {
						return false;
					}
					break;

				default:
					break;
				}
				return true;
			}
		});

		mF1.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent e) {
				// TODO Auto-generated method stub

				switch (e.getAction()) {
				case MotionEvent.ACTION_DOWN:
					if (e.getX() < 100) {
						mS1.setX(100 - 30);
					} else if (e.getX() > 610) {
						mS1.setX(610 - 30);
					} else {
						mS1.setX(e.getX() - 30);
					}
					currentTime = System.currentTimeMillis();
					Log.i("wmy", "按下坐标 x y = " + e.getX() + " " + e.getY());
					mS1.postInvalidate();
					break;
				case MotionEvent.ACTION_MOVE:
					if (e.getX() < 100) {
						mS1.setX(100 - 30);
						level = 0;
					} else if (e.getX() > 610) {
						mS1.setX(610 - 30);
						level = 100;
					} else {
						mS1.setX(e.getX() - 30);
						level = (int) ((e.getX() - 100) * 100 / 510);
					}
					currentTime = System.currentTimeMillis();
					if (currentTime - lastTime > 150) {
						lastTime = currentTime;
						Log.i("wmy", "level = " + level);
					}
					mS1.postInvalidate();
					break;
				case MotionEvent.ACTION_UP:
					if (e.getX() < 100) {
						mS1.setX(100 - 30);
					} else if (e.getX() > 610) {
						mS1.setX(610 - 30);
					} else {
						mS1.setX(e.getX() - 30);
					}
					mS1.postInvalidate();
					break;

				default:
					break;
				}
				return true;
			}
		});
	}

	@Override
	public void setListener() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doBusiness(Context mContext) {
		// TODO Auto-generated method stub

	}

	public void boolVersion() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

			Window window = getWindow();
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// 透明导航栏
//			getWindow().addFlags(
//					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
			// 改变titlebar的高度
			int statusbarHeight = BarUtils.getStatusBarHeight(this);
			RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) mStatusBar
					.getLayoutParams();
			layoutParams1.height = statusbarHeight;
			mStatusBar.setLayoutParams(layoutParams1);
			mStatusBar.setVisibility(View.VISIBLE);
		} else {
			mStatusBar.setVisibility(View.GONE);
		}
	}

	public static Bitmap drawableToBitmap(Drawable drawable) {
		Bitmap bitmap = Bitmap
				.createBitmap(
						664,
						664,
						drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
								: Bitmap.Config.ARGB_4444);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, 664, 664);
		drawable.draw(canvas);
		return bitmap;
	}

	/**
	 * 计算以(src_x,src_y)为坐标圆点，建立直角体系，求出(target_x,target_y)坐标与x轴的夹角
	 * 主要是利用反正切函数的知识求出夹角
	 * 
	 * @param src_x
	 * @param src_y
	 * @param target_x
	 * @param target_y
	 * @return
	 */
	float detaDegree(float src_x, float src_y, float target_x, float target_y) {
		float detaX = target_x - src_x;
		float detaY = target_y - src_y;
		double d;
		if (detaX != 0) {
			float tan = Math.abs(detaY / detaX);

			if (detaX > 0) {

				if (detaY >= 0) {
					d = Math.atan(tan);

				} else {
					d = 2 * Math.PI - Math.atan(tan);
				}

			} else {
				if (detaY >= 0) {

					d = Math.PI - Math.atan(tan);
				} else {
					d = Math.PI + Math.atan(tan);
				}
			}

		} else {
			if (detaY > 0) {
				d = Math.PI / 2;
			} else {

				d = -Math.PI / 2;
			}
		}
		// Log.i("wmy", "夹角为" + (d * 180) / Math.PI);
		return (float) ((d * 180) / Math.PI);
	}

	private void getButtonXY(float degree) {
		tempX1 = (int) (o_x + (200 * Math.cos(Math.toRadians(degree))));
		tempY1 = (int) (o_y + (200 * Math.sin(Math.toRadians(degree))));
	}

	private void getColor() {
		pixel = bm.getPixel(tempX1 - 12, tempY1 - 12);
		r = Color.red(pixel);
		b = Color.blue(pixel);
		g = Color.green(pixel);
//		Log.i("wmy", "red green blue= " + r + " " + g + " " + b);
	}

	private boolean adjustArea(float src_x, float src_y, float target_x,
			float target_y) {
		inner = (int) Math.sqrt(Math.pow(Math.abs(target_x - src_x), 2)
				+ Math.pow(Math.abs(target_y - src_y), 2));
		if (inner < 300 && inner > 90) {
			return true;
		} else {
			return false;
		}
	}

}
